<?php

namespace App\Core;

use App\Core\Exception\DefaultException;
use EasySwoole\Http\Message\Status;

trait OutputTrait{
    /**
     * 成功返回
     * @param array $data 响应数据
     * @param string $message 提示信息
     * @return Output
     */
    public function success($data = [], $message = 'ok'){
        return (new Output())->setStatusCode(Status::CODE_OK)->setMsg($message)->setData($data);
    }
    
    /**
     * 失败返回
     * @param int|string $statusCode 错误码
     * @param string $message 提示信息
     * @param array $data 响应数据
     * @return Output
     */
    public function error($statusCode, $message = '', $data = []){
        return (new Output())->setStatusCode($statusCode)->setMsg($message)->setData($data);
    }
    
    /**
     * 失败异常抛出封装
     * @param int|string $statusCode 错误码
     * @param null|string $message 提示信息
     * @throws DefaultException
     */
    public function exception($statusCode, $message = ''){
        throw new DefaultException($message, $statusCode);
    }
}